<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车辆类型" prop="vehicleType">
        <el-select v-model="queryParams.vehicleType" placeholder="车辆类型" clearable>
          <el-option
            v-for="dict in dict.type.lg_vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车辆长度(m)" prop="vehicleLength">
        <el-input
          v-model="queryParams.vehicleLength"
          placeholder="请输入车辆长度(m)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆容积(m³)" prop="vehicleVolume">
        <el-input
          v-model="queryParams.vehicleVolume"
          placeholder="请输入车辆容积(m³)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆状态" prop="vehicleStatus">
        <el-select v-model="queryParams.vehicleStatus" placeholder="车辆状态" clearable>
          <el-option
            v-for="dict in dict.type.equipment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

        <el-form-item label="驾驶证类型" prop="licenseType">
          <el-select v-model="queryParams.licenseType" placeholder="驾驶证类型" clearable>
            <el-option
              v-for="dict in dict.type.lg_driver_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      <!-- 创建时间范围选择 -->
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['motor:vehicle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['motor:vehicle:update']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['motor:vehicle:delete']"
        >删除</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['motor:vehicle:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getVehicleList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />

      <el-table-column label="车辆类型" align="center" prop="vehicleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_vehicle_type" :value="scope.row.vehicleType"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆长度(m)" align="center" prop="vehicleLength" />
      <el-table-column label="车辆容积(m³)" align="center" prop="vehicleVolume" />
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="车辆状态" align="center" prop="vehicleStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_status" :value="scope.row.vehicleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="驾驶证类型" align="center" prop="licenseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lg_driver_type" :value="scope.row.licenseType"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['motor:vehicle:update']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['motor:vehicle:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getTotalSome"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车辆类型" prop="vehicleType">
          <el-radio-group v-model="form.vehicleType">
            <el-radio
              v-for="dict in dict.type.lg_vehicle_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆长度(m)" prop="vehicleLength">
          <el-input v-model="form.vehicleLength" placeholder="请输入车辆长度(m)" />
        </el-form-item>
        <el-form-item label="车辆容积(m³)" prop="vehicleVolume">
          <el-input v-model="form.vehicleVolume" placeholder="请输入车辆容积(m³)" />
        </el-form-item>
        <el-form-item label="车牌号" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
        </el-form-item>

        <el-form-item label="车辆状态" prop="vehicleStatus">
          <el-radio-group v-model="form.vehicleStatus">
            <el-radio
              v-for="dict in dict.type.equipment_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="驾驶证类型" prop="licenseType">
          <el-radio-group v-model="form.licenseType">
            <el-radio
              v-for="dict in dict.type.lg_driver_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {

  listVehicle,
  getTotal,

  getVehicleById,
  updateVehicle, addVehicle, delVehicle
} from "@/api/motor/vehicle";

import store from "@/store";



export default {
  name: "Vehicle",
  dicts: ['equipment_status','lg_vehicle_type','lg_driver_type'],
  data() {
    return {
      userId:store.state.user.id, //将缓存的用户id赋值给userId
      userName:store.state.user.name, //将缓存的用户id赋值给userId
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
// 日期范围
      dateRange: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      // postList: [],
      // 岗位表格数据
      vehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vehicleType: undefined,
        vehicleLength: undefined,
        vehicleVolume: undefined,
        licenseType: undefined,
        licensePlate: undefined,
        beginTime: undefined,
        endTime: undefined,
        vehicleStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        vehicleLength: [
          { required: true, message: "车辆长度不能为空", trigger: "blur" }
        ],
        vehicleVolume: [
          { required: true, message: "车辆容积不能为空", trigger: "blur" }
        ],
        licensePlate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getVehicleList();
    this.getTotalSome();
  },
  methods: {
    /** 查询车辆列表 */
    getVehicleList() {
      this.loading = true;
      this.queryParams.beginTime = this.dateRange[0];
      this.queryParams.endTime = this.dateRange[1];
      listVehicle(this.queryParams).then(response => {
        this.vehicleList = response.data;
        this.loading = false;
      });
      this.loading = false;
    },
    //获取数据总条数
    getTotalSome() {
      this.loading = true;
      getTotal(this.queryParams).then(response => {
        this.getVehicleList();
        this.total = response.data;
        this.loading = false;
      });
      this.loading = false;
    },
   // getVehicleById


    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleType: "1",
        vehicleLength: undefined,
        vehicleVolume: undefined,
        licensePlate: undefined,
        licenseType: "1",
        vehicleStatus: "0",
        deleteStatus: null,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getTotalSome();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vehicleId = row.vehicleId || this.ids
      getVehicleById(vehicleId).then(response => {
        this.form = response.data;
         this.form.vehicleStatus = response.data.vehicleStatus.toString();
        this.form.vehicleType = response.data.vehicleType.toString();
        this.form.licenseType = response.data.licenseType.toString();
        this.open = true;
        this.title = "修改车辆";
      });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleId != undefined) {
            // 添加修改人
            this.form.updateBy = this.userName;
            updateVehicle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getVehicleList();
            });
          } else {
            //添加创建人
            this.form.createBy = this.userName;
            addVehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getVehicleList();
              this.form = {};
            });
          }
        }
      });
    },

    /** 删除按钮操作 */

    handleDelete(row) {
      const vehicleIds = row.vehicleId || this.ids;
      this.$modal.confirm('是否确认删除此数据项？').then(function() {
        return delVehicle(vehicleIds);  // 使用 DELETE 请求
      }).then(() => {
        this.getVehicleList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});

    },

    //


    /** 导出按钮操作 */
    handleExport() {
      this.download('motor/vehicle/export', {
        ...this.queryParams
      }, `vehicle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
